#include "Coordinate.h"

struct Vertex
{
	float x, y, z;
	DWORD color;
	static const DWORD FVF = D3DFVF_XYZ | D3DFVF_DIFFUSE;
};

Coordinate::Coordinate(IDirect3DDevice9* Device, float width, float height)
{
	width /= 2;
	height /= 2;
	HRESULT hresult{ };
	Vertex data[] =
	{
		{-width, 0.0f, 0.1f, D3DCOLOR_XRGB(255, 0, 0)},
		{ width, 0.0f, 0.1f, D3DCOLOR_XRGB(0, 255, 0)},
		{0.0f, -height, 0.1f, D3DCOLOR_XRGB(255, 0, 0)},
		{0.0f,  height, 0.1f, D3DCOLOR_XRGB(0, 255, 0)},
	};

	hresult = Device->CreateVertexBuffer(
		sizeof(data),
		0,
		Vertex::FVF,
		D3DPOOL_DEFAULT,
		&_VertexBuffer,
		nullptr
	);
	if (FAILED(hresult))
		throw std::runtime_error("");
	void* ptr;
	hresult = _VertexBuffer->Lock(0, sizeof(data), &ptr, 0);
	if (FAILED(hresult))
		throw std::runtime_error("");
	memcpy(ptr, data, sizeof(data));
	_VertexBuffer->Unlock();
}

Coordinate::~Coordinate()
{

}

void Coordinate::Update()
{

}

void Coordinate::Draw(IDirect3DDevice9* Device)
{
	D3DXMATRIX matWorld;
	D3DXMatrixTranslation(&matWorld, 0.0f, 0.0f, 0.0f);

	Device->SetTransform(D3DTS_WORLD, &matWorld);

	Device->SetStreamSource(0, _VertexBuffer.Get(), 0, sizeof(Vertex));
	Device->SetFVF(Vertex::FVF);
	Device->DrawPrimitive(D3DPT_LINELIST, 0, 2);
}
